package com.yztc.orangeentertainment.ui.main.findfragment.findadapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.bean.findbean.FindStarBean;
import com.yztc.orangeentertainment.ui.main.findfragment.findutils.CustomShapeTransformation;
import com.yztc.orangeentertainment.ui.main.subscribefragment.staritem.StarItemActivity;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/8.
 */
public class FindStarRlvAdapter extends RecyclerView.Adapter<FindStarRlvAdapter.StarViewHolder> {
    private Context context;
    private List<FindStarBean> list;

    public FindStarRlvAdapter(Context context, List<FindStarBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public StarViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        StarViewHolder holder = new StarViewHolder(LayoutInflater.from(context).inflate(R.layout
                .find_starrlv, null));
        return holder;
    }

    @Override
    public void onBindViewHolder(StarViewHolder holder, int position) {
        int rank = list.get(position).getRank();
        if (rank == 1) {
            position = 1;
            holder.showIv.setImageResource(R.mipmap.ic_star_rank_two);
            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) holder.starIv
                    .getLayoutParams();
            params.height = params.height * 3 / 4;
            params.width = params.width * 3 / 4;
            holder.starIv.setLayoutParams(params);
            holder.starFrm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, StarItemActivity.class);
                    intent.putExtra("sid", list.get(1).getId() + "");
                    context.startActivity(intent);
                }
            });

        } else if (rank == 2) {
            position = 0;
            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) holder.showIv
                    .getLayoutParams();
            params.height = params.height * 4 / 3;
            params.width = params.width * 4 / 3;
            holder.showIv.setLayoutParams(params);
            holder.showIv.setImageResource(R.mipmap.ic_star_rank_one);
            holder.starFrm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, StarItemActivity.class);
                    intent.putExtra("sid", list.get(0).getId() + "");
                    context.startActivity(intent);
                }
            });

        } else if (rank == 3) {
            position = 2;
            holder.showIv.setImageResource(R.mipmap.ic_star_rank_three);
            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) holder.starIv
                    .getLayoutParams();
            params.height = params.height * 3 / 4;
            params.width = params.width * 3 / 4;
            holder.starIv.setLayoutParams(params);
            holder.starFrm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, StarItemActivity.class);
                    intent.putExtra("sid", list.get(2).getId() + "");
                    context.startActivity(intent);
                }
            });

        }
        holder.showTvname.setText(list.get(position).getName());
        holder.showTveng.setText(list.get(position).getEngname());
        Glide.with(context).load(list.get(position).getPortrait()).bitmapTransform(new
                CustomShapeTransformation(context, R.mipmap.ic_discover_staravatar_bg)).into
                (holder.starIv);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class StarViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.star_iv)
        ImageView starIv;
        @InjectView(R.id.show_iv)
        ImageView showIv;
        @InjectView(R.id.star_frm)
        FrameLayout starFrm;
        @InjectView(R.id.show_tvname)
        TextView showTvname;
        @InjectView(R.id.show_tveng)
        TextView showTveng;

        public StarViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }


}
